package com.sy.service.impl;

import com.sy.entity.Admin;
import com.sy.exception.BusinessException;
import com.sy.mapper.AdminMapper;
import com.sy.service.AdminService;
import com.sy.util.RedisOperations;
import com.sy.vo.AdminVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.Objects;

import static com.sy.constant.BusinessConstant.REDIS_VALIDATE_CODE_PREFIX;
import static com.sy.constant.RegexConstant.*;

/**
 * @Author:cxl
 * @Date: 2021/10/12 9:55
 */
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Autowired
    private RedisOperations redisOperations;


    @Override
    public boolean isAdminNameExists(String adminName) {
        if (adminName.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员姓名不能为空");
        }
       if(Objects.isNull(adminMapper.getAdminByAdminName(adminName))){
           throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员姓名不存在");
       }
       return true;
    }

    @Override
    public boolean isAdminPhoneExists(String phone) {
        if (phone.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员手机不能为空");
        }
        if(Objects.isNull(adminMapper.getAdminByPhone(phone))){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员手机不存在");
        }
        return true;
    }

    @Override
    public boolean isAdminEmailExists(String email) {
        if (email.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员邮箱不能为空");
        }
        if(Objects.isNull(adminMapper.getAdminByEmail(email))){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员邮箱不存在");
        }
        return true;
    }


    @Override
    public boolean addAdmin(AdminVo adminVo) {
        String name = adminVo.getName();
        String phone = adminVo.getPhone();
        String email = adminVo.getEmail();
        String pwd = adminVo.getPwd();
        String validateCode = adminVo.getValidateCode();
        if (name.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员姓名不能为空");
        }
        if (!Objects.isNull(adminMapper.getAdminByAdminName(name))){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员姓名已存在");
        }
        if(phone.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员手机不能为空");
        }
        if (!phone.matches(PHONE_REGEX)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员手机不合法");
        }
        if (!Objects.isNull(adminMapper.getAdminByPhone(phone))) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员手机已存在");
        }
        if (email.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员邮箱不能为空");
        }
        if (!email.matches(EMAIL_REGEX)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员邮箱不合法");
        }
        if(!Objects.isNull(adminMapper.getAdminByEmail(email))){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员邮箱已存在");
        }
        if(pwd.isEmpty()){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员密码不能为空");
        }
        if(!pwd.matches(STRONG_PWD_REGEX)){
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员密码需为强密码");
        }
        //验证码是否为空
        if (StringUtils.isBlank(validateCode)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "验证码不能为空");
        }
        //验证码是否和实际发送到手机的一致
        //从Redis中获取当前手机号对应的验证码的信息
        String correctValidateCode = redisOperations.get(REDIS_VALIDATE_CODE_PREFIX + phone);
        //(1)从Redis中没有获取到当前手机号对应的验证码信息-验证码可能失效了（或者压根没法）
        if (StringUtils.isBlank(correctValidateCode)) {
            throw new BusinessException(HttpStatus.BAD_REQUEST, "验证码可能已经失效了，请重新发送~");
        }
        //(2)从Redis中获取到手机号对应的验证码信息
        else {
            //(2-1)用户输入的和Redis中的不相等-验证码输入错误的情况
            if (!StringUtils.equals(correctValidateCode, validateCode)) {
                throw new BusinessException(HttpStatus.BAD_REQUEST, "验证码错误~");
            }
        }
        Admin admin = new Admin();
        admin.setName(name);
        admin.setEmail(email);
        admin.setPhone(phone);
        admin.setPwd(pwd);
        boolean success = adminMapper.addAdmin(admin);
        if (success){
            return success;
        }else{
            throw new BusinessException(HttpStatus.BAD_REQUEST, "管理员注册失败");
        }

    }
}
